from datetime import datetime
from . import db


# 分类电影关系表
class RelaCategroyMovie(db.Model):
    __tablename__ = "relacategroymovietable"

    id = db.Column(db.Integer, primary_key=True)
    categroy_id = db.Column(db.Integer, db.ForeignKey('categroymovietable.numid'))
    movie_id = db.Column(db.Integer, db.ForeignKey('movietable.id'))


tb_categroy_movie = db.Table(
    'tb_categroy_movie',
    db.Column('categroy_id', db.Integer, db.ForeignKey('categroymovietable.numid')),
    db.Column('movie_id', db.Integer, db.ForeignKey('movietable.id'))
)


class CategroyMovie(db.Model):
    __tablename__ = "categroymovietable"

    numid = db.Column(db.Integer, primary_key=True)
    categroy = db.Column(db.VARCHAR(100))
    url = db.Column(db.VARCHAR(100))
    title = db.Column(db.VARCHAR(100))
    source = db.Column(db.VARCHAR(100))
    movies = db.relationship(
        'MovieTable',
        secondary=tb_categroy_movie,
        backref='ctgr',
        lazy='dynamic'
    )

    def to_json(self):
        json = {
            'numid': self.numid,
            'categroy': self.categroy,
            'url': self.url,
            'title': self.title,
            'source': self.source,
        }
        return json


class MovieTable(db.Model):
    __tablename__ = "movietable"

    id = db.Column(db.Integer, primary_key=True)
    moviename = db.Column(db.VARCHAR(100))
    time = db.Column(db.VARCHAR(100))
    url = db.Column(db.VARCHAR(500))
    imagepath = db.Column(db.VARCHAR(2000))
    saveumagepath = db.Column(db.VARCHAR(500))
    score = db.Column(db.FLOAT)
    status = db.Column(db.Integer)
    source = db.Column(db.VARCHAR(10))
    categroy = db.relationship(
        'CategroyMovie',
        secondary=tb_categroy_movie,
        backref='mv',
        lazy='dynamic'
    )
    movie_performer = db.relationship(
        'MoviePerformerTable',
        backref='mvp',
        lazy='dynamic'
    )

    def to_json(self):
        json = {
            'id': self.id,
            'time': self.time,
            'url': self.url,
            'imagepath': self.imagepath,
            'saveumagepath': self.saveumagepath,
            'score': self.score,
            'status': self.status,
            'source': self.source,
            'moviename': self.moviename
        }
        return json


class MovieDetailTable(db.Model):
    __tablename__ = "moviedetailtable"

    id = db.Column(db.Integer, primary_key=True)
    director = db.Column(db.VARCHAR(100))
    keyword = db.Column(db.VARCHAR(500))
    categroy = db.Column(db.VARCHAR(500))
    des = db.Column(db.VARCHAR(3000))


class MoviePerformerTable(db.Model):
    __tablename__ = "movieperformertable"

    id = db.Column(db.Integer, db.ForeignKey('movietable.id'))
    i = db.Column(db.Integer, primary_key=True)
    performer = db.Column(db.VARCHAR(100))
    role = db.Column(db.VARCHAR(255))


class MovieUrl(db.Model):
    __tablename__ = "movieurl"

    id = db.Column(db.Integer, primary_key=True)
    movieurl = db.Column(db.VARCHAR(255))


class PerformerDetailTable(db.Model):
    __tablename__ = "performerdetailtable"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.VARCHAR(100))
    e_name = db.Column(db.VARCHAR(100))
    alias = db.Column(db.VARCHAR(200))
    sex = db.Column(db.VARCHAR(10))
    bloodtype = db.Column(db.VARCHAR(5))
    height = db.Column(db.VARCHAR(10))
    address = db.Column(db.VARCHAR(500))
    birthday = db.Column(db.VARCHAR(50))
    constellation = db.Column(db.VARCHAR(500))
    location = db.Column(db.VARCHAR(200))
    ResidentialAddress = db.Column(db.VARCHAR(100))
    school = db.Column(db.VARCHAR(200))
    BrokerageAgency = db.Column(db.VARCHAR(200))
    fameyear = db.Column(db.VARCHAR(200))
    hobby = db.Column(db.VARCHAR(1000))
    Occupation = db.Column(db.VARCHAR(500))
    weight = db.Column(db.VARCHAR(500))
    image = db.Column(db.VARCHAR(1000))
    des = db.Column(db.VARCHAR(2000))
